package com.ymatou.iapi.liveseller.testcase;

import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.SysCloseLiveBean;
import com.ymatou.iapi.liveseller.service.SysCloseLiveCall;
import com.ymatou.livevideo.parameter.StreamLiveNotifyBean;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.YMTDateUtil;
import org.apache.http.client.ClientProtocolException;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
*系统关闭
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SysCloseLive{
	private static SysCloseLiveCall syscloseliveCall;
	private static SysCloseLiveBean syscloseliveBean;
	private static LiveRoomWapper liveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("系统关闭");
	}

	@Before
	public void caseUp() {
		syscloseliveCall=new SysCloseLiveCall();
		syscloseliveBean=new SysCloseLiveBean();
		liveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(syscloseliveCall.getOpurl());
		liveRoomWapper=null;
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SysCloseLive_001() {
		Logger.start(true,"断流状态超出结束时间系统关闭成功");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			
			StreamLiveNotifyBean streamlivenotifyBean = LiveRoomCallService.initStreamLiveNotifyBean(createlive.getId(), 0);
			LiveRoomCallService.LiveNotify(streamlivenotifyBean);
			
			liveRoomWapper.updateLiveRoomTime(YMTDateUtil.getBeforeOrNextMunite(-100), YMTDateUtil.getBeforeOrNextMunite(-50), YMTDateUtil.getBeforeOrNextMunite(-50),createlive.getId());
			syscloseliveCall.setData(syscloseliveBean);
			syscloseliveCall.callService();			
			Logger.verifyEquals("ok", syscloseliveCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(7, liveroom.getLiveStatus(), "验证系统自动关闭");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SysCloseLive_002() {
		Logger.start(true,"断流状态不超出结束时间系统不关闭");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			
			StreamLiveNotifyBean streamlivenotifyBean = LiveRoomCallService.initStreamLiveNotifyBean(createlive.getId(), 0);
			LiveRoomCallService.LiveNotify(streamlivenotifyBean);
			
			liveRoomWapper.updateLiveRoomTime(YMTDateUtil.getBeforeOrNextMunite(-10), YMTDateUtil.getBeforeOrNextMunite(50),YMTDateUtil.getBeforeOrNextMunite(-50), createlive.getId());
			syscloseliveCall.setData(syscloseliveBean);
			syscloseliveCall.callService();			
			Logger.verifyEquals("ok", syscloseliveCall.getReturnData(), "验证返回");
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			
			Logger.verifyEquals(3, liveroom.getLiveStatus(), "验证依然在断流状态");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SysCloseLive_003() {
		Logger.start(true,"推流状态超出结束时间系统不关闭");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			
			liveRoomWapper.updateLiveRoomTime(YMTDateUtil.getBeforeOrNextMunite(-100), YMTDateUtil.getBeforeOrNextMunite(-50), YMTDateUtil.getBeforeOrNextMunite(-50),createlive.getId());
			syscloseliveCall.setData(syscloseliveBean);
			syscloseliveCall.callService();			
			Logger.verifyEquals("ok", syscloseliveCall.getReturnData(), "验证返回");
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			
			Logger.verifyEquals(2, liveroom.getLiveStatus(), "验证系统推流");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SysCloseLive_004() {
		Logger.start(true,"8关闭直播不修改状态");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 8);
			syscloseliveCall.callService();			
			syscloseliveCall.setData(syscloseliveBean);
			syscloseliveCall.callService();			
			Logger.verifyEquals("ok", syscloseliveCall.getReturnData(), "验证返回");
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			
			Logger.verifyEquals(8, liveroom.getLiveStatus(), "验证系统关闭8");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SysCloseLive_005() {
		Logger.start(true,"9结束直播不修改状态");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 9);
			syscloseliveCall.callService();			
			syscloseliveCall.setData(syscloseliveBean);
			syscloseliveCall.callService();			
			Logger.verifyEquals("ok", syscloseliveCall.getReturnData(), "验证返回");
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			
			Logger.verifyEquals(9, liveroom.getLiveStatus(), "验证系统关闭8");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SysCloseLive_006() {
		Logger.start(true,"10强制关停不修改状态");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 10);
			syscloseliveCall.callService();			
			syscloseliveCall.setData(syscloseliveBean);
			syscloseliveCall.callService();			
			Logger.verifyEquals("ok", syscloseliveCall.getReturnData(), "验证返回");
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			
			Logger.verifyEquals(10, liveroom.getLiveStatus(), "验证系统关闭8");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SysCloseLive_007() {
		Logger.start(true,"白名单客户");
		try {
			int uid=512003;
			CreateSellerLiveBean createSellerLiveBean = LiveRoomCallService.initCreatesellerliveBean(uid);
			//disconf白名单
			createSellerLiveBean.getLiveDto().setUserId(uid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(createSellerLiveBean,true);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 3);
			liveRoomWapper.updateLiveRoomEndTimeAndUpdateTime(YMTDateUtil.getBeforeOrNextMunite(-20), YMTDateUtil.getBeforeOrNextMunite(-20), createlive.getId());
			syscloseliveCall.callService();
			syscloseliveCall.setData(syscloseliveBean);
			syscloseliveCall.callService();
			Thread.sleep(2000);
			Logger.verifyEquals("ok", syscloseliveCall.getReturnData(), "验证返回");
			Logger.verifyEquals(7, liveRoomWapper.selectLiveRoomById(createlive.getId()).getLiveStatus(),"验证状态");

			verifyMongoCurMq(createlive.getId());

        } catch (Exception e) {
			Logger.fail(e);
		}
	}

	public void verifyMongoCurMq(int liveId) throws ClientProtocolException, IOException, InterruptedException {
		Thread.sleep(1000);
		MessageListBean MessageListBean2 = MqQueryCallService.initSteamSync(liveId);
		List<Map> joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
		Logger.debug(joa2);
		Logger.verifyEquals(7,Integer.parseInt(joa2.get(0).get("LiveStatus").toString()),"验证LiveStatus");
		Logger.verifyEquals(liveId,Integer.parseInt(joa2.get(0).get("LiveId").toString()),"验证LiveId");
	}
}